﻿/* Serialize Plugin Context
 * Copyright Andrew Swerlick - Engage, Inc. 2010
 * is free software: you can redistribute it and/or modify it under the terms of the 
 * GNU General Public License as published by the Free Software Foundation, 
 * either version 3 of the License, or (at your option) any later version.
 * Serialize Plugin Context is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License along with Serialize Plugin Context. 
 * If not, see http://www.gnu.org/licenses/.
 ***********************************************/

using System;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;

using System.ServiceModel.Description;

namespace Engage.Util.PluginTester
{
    public class MyOrganizationServiceFactory : IOrganizationServiceFactory
    {
        public OrganizationServiceProxy service;
        public string UserName { get; set; }
        public string Password { get; set; }
        public string ServerName { get; set; }

        /// <summary>
        /// Connects to the CRM organizationServiceProxy using the username password and service name of the MyOrganizaitonServiceFactory
        /// </summary>
        public void SetService()
        {
            ClientCredentials clientCreds = new ClientCredentials();
            clientCreds.UserName.UserName = this.UserName;
            clientCreds.UserName.Password = this.Password;
            this.service = new OrganizationServiceProxy(new Uri(ServerName), null, clientCreds, null);
        }
        #region IOrganizationServiceFactory Members

        public IOrganizationService CreateOrganizationService(Guid? userId)
        {
            return service;
        }

        #endregion
    }
}
